package com.naiterui.ehp.bs.patient.rpc.im;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.bo.im.UserCommonParams;
import com.naiterui.ehp.bp.bo.im.UserSessionStatusBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;
import com.naiterui.ehp.bs.patient.rpc.BaseRemoter;

import lombok.extern.slf4j.Slf4j;

/**
 * 会话相关逻辑远程调用
 *
 * @author Amber
 * @since 1.0.0
 */
@Slf4j
@Service
public class ConsultSessionRemoter extends BaseRemoter implements IConsultSessionRemoter {

    @Override
    public Map<Long, Boolean> aliveMap(Long patientId, Long... doctorIds) throws BusinessException {
        Map<Long, Boolean> aliveMap = new HashMap<Long, Boolean>();
        // 医生或患者id为空，返回false
        if (patientId == null || doctorIds == null) {
            return aliveMap;
        }

        UserCommonParams params = new UserCommonParams();
        params.setPatientId(patientId);
        params.setDoctorIds(Arrays.asList(doctorIds));
        try {
            String body = JsonMapper.toJson(params);
            // 接口地址
            String url = this.apRemoterConfig.getAliveConsultSession();
            List<UserSessionStatusBO> sessionList = RestHttpUtil.post()
                                                                .url(url)
                                                                .body(body)
                                                                .executeData(new ParameterizedTypeReference<ResponseVO<List<UserSessionStatusBO>>>() {
                                                                });
            return sessionList.stream().collect(Collectors.toMap(UserSessionStatusBO::getDoctorId, UserSessionStatusBO::getAlive, (a, b) -> b));
        } catch (Exception e) {
            log.error("获取医生:{}，患者:{}之间是否有进行中会话失败;异常信息:{}", doctorIds, patientId, e);
            return aliveMap;
        }

    }

}
